﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;
using MQBasic.Config;
using MQBasic.Entity;
using Newtonsoft.Json;

namespace MQProcedurer
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ConnectionFactory factory = new ConnectionFactory();
                factory.HostName = MQConfig.MqHost;
                factory.Port = MQConfig.MqPort;
                factory.UserName = MQConfig.MqUserName;
                factory.Password = MQConfig.MqPwd;
                using (IConnection conn = factory.CreateConnection())
                {
                    using (IModel channel = conn.CreateModel())
                    {
                        int queCount = 1; //队列个数

                        //在MQ上定义一个持久化队列，如果名称相同不会重复创建
                        for (int i = 0; i < queCount; i++)
                        {
                            channel.QueueDeclare(MQConfig.QueueName + "_" + i.ToString(), true, false, false, null);
                        }
                       

                        for(int i = 1; i <= 10000; i++)
                        {
                            int queM = i % queCount;
                            TraceMsg traceMsg = new TraceMsg();
                            traceMsg.ClientKey = i.ToString().PadLeft(5, '0');
                            traceMsg.GpsDate = DateTime.Now;
                            traceMsg.OnlineTime = i;

                            string jsonStr = JsonConvert.SerializeObject(traceMsg);
                            byte[] bytes = Encoding.UTF8.GetBytes(jsonStr);

                            //设置消息持久化
                            IBasicProperties properties = channel.CreateBasicProperties();
                            properties.DeliveryMode = 2;
                            channel.BasicPublish("", MQConfig.QueueName + "_" + queM.ToString(), properties, bytes);

                            //channel.BasicPublish("", "MyFirstQueue", null, bytes);

                            Console.WriteLine("消息已发送地{0}条消息。", i);
                        }
                       
                    }
                }
            }
            catch (Exception e1)
            {
                Console.WriteLine(e1.ToString());
            }
            Console.ReadLine();
        }
    }
}
